package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallGoodsAttributeMapper;
import org.linlinjava.litemall.db.domain.LitemallGoodsAttribute;
import org.linlinjava.litemall.db.domain.LitemallGoodsAttributeExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallGoodsAttributeService {
    @Resource
    private LitemallGoodsAttributeMapper goodsAttributeMapper;

    public List<LitemallGoodsAttribute> queryByGid(Integer goodsId) {
        LitemallGoodsAttributeExample example = new LitemallGoodsAttributeExample();
        example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return goodsAttributeMapper.selectByExample(example);
    }

    public void add(LitemallGoodsAttribute goodsAttribute) {
        goodsAttribute.setAddTime(LocalDateTime.now());
        goodsAttribute.setUpdateTime(LocalDateTime.now());
        goodsAttributeMapper.insertSelective(goodsAttribute);
    }


    public void validAndAdd(LitemallGoodsAttribute goodsAttribute) {

        LitemallGoodsAttributeExample example = new LitemallGoodsAttributeExample();
        example.or().andGoodsIdEqualTo(goodsAttribute.getGoodsId()).andAttributeEqualTo(goodsAttribute.getAttribute()).andValueEqualTo(goodsAttribute.getValue());
        LitemallGoodsAttribute list = goodsAttributeMapper.selectOneByExample(example);
        if(list==null || list.getId()<=0){
            goodsAttribute.setAddTime(LocalDateTime.now());
            goodsAttribute.setUpdateTime(LocalDateTime.now());
            goodsAttributeMapper.insertSelective(goodsAttribute);
        }else {
            list.setDeleted(false);
            list.setUpdateTime(LocalDateTime.now());
            goodsAttributeMapper.updateByPrimaryKey(list);

        }

    }

    public LitemallGoodsAttribute findById(Integer id) {
        return goodsAttributeMapper.selectByPrimaryKey(id);
    }

    public void deleteByGid(Integer gid) {
        LitemallGoodsAttributeExample example = new LitemallGoodsAttributeExample();
        example.or().andGoodsIdEqualTo(gid);
        goodsAttributeMapper.logicalDeleteByExample(example);
    }
}
